/*
package com.lang.object;

import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
import sun.jvm.hotspot.utilities.SystemDictionaryHelper;

import java.io.File;
import java.io.PrintStream;


*/
/**
 * 需要导入 sa-jdi jar包 路径在 jdk/lib/sa-jdi.jar
 * 打印对象的内存布局
 *//*

public class ObjectMemoryMonitor extends Tool {
    public static void main(String[] args) throws InterruptedException {
        ObjectMemoryMonitor test = new ObjectMemoryMonitor();
//        test.start(args);
        args = new String[]{"10060"};
        test.execute(args);
//        test.run();
        test.stop();
    }

    @Override
    public void run() {
        try {
            File file = new File("./temp");
            PrintStream out = new PrintStream(file);
            System.setOut(out);

            VM vm = VM.getVM();
            ObjectHeap objHeap = vm.getObjectHeap();
            HeapVisitor heapVisitor = new HeapPrinter(System.out);
            //观察特定对象
            Klass klass = SystemDictionaryHelper.findInstanceKlass("com.lang.object.ObjectMemoryLayout$MyClass");
            objHeap.iterateObjectsOfKlass(heapVisitor, klass, false);

            //观察数组对象
            objHeap.iterate(heapVisitor, new ObjectHeap.ObjectFilter() {
                @Override
                public boolean canInclude(Oop oop) {
                    return oop.isObjArray();
                }
            });
            objHeap.iterate(heapVisitor);
        }catch (Exception e){
            e.getMessage();
        }finally {
        }
    }

}*/
